package com.dnjn.happiness.emo.webapi.hcm;

import com.alibaba.fastjson2.JSON;
import com.dnjn.happiness.emo.webapi.util.JwtUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HCM_SAPHRClient {


    public static void main(String[] args) throws IOException {
        final   String JWT_KEY = "WPMS-GT3X0PS2";// "WPMS-F05QWIDP";
          final   String JWT_SECRET = "a7178bd1-5f65-4164-a2d7-c7f03f75efa4";//"3607511f-a508-4040-9b3b-70fb6513e439";

        //String host = "http://172.16.202.34:8000";
        String host = "http://jip.jinkosolar.com";
        final LocalDate startDate = LocalDate.of(2023, 1, 11);
        final LocalDate endDate = LocalDate.of(2024, 3, 12);
        String resp = loadQxPtData(host, startDate, endDate,JWT_KEY,JWT_SECRET);
        System.out.println(resp);

    }

    public static String loadQxPtData(  String host, LocalDate startDate, LocalDate endDate,String key, String secret) throws IOException {
        String token = JwtUtil.createJwtToken(key, secret);
        OkHttpClient client = okHttpClient();

        Map<String, Object> params = new HashMap<>();
        params.put("IV_BEGDA", startDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        params.put("IV_ENDDA", endDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")));

        MediaType mediaType = MediaType.parse("application/json");
        final String content = JSON.toJSONString(params);
        RequestBody body = RequestBody.create(mediaType, content);

//        RequestBody.create(MediaType.parse("application/json"), JSON.toJSONString(params))
        System.out.println(content);


        final String auth = "Bearer " + token;
        Map<String, String> headers = Maps.newHashMap();
        headers.put("Authorization", auth);
        headers.put("Content-Type", "application/json");
        String header_str = "";
        for (String k : headers.keySet()) {
            String v = headers.get(k);
            header_str += k + ": " + v+"\n";
        }
        Request request = new Request.Builder()
                .url(host + "/JIP/HCM/HCM_QX_PTDATA")
                .method("POST", body)
                .headers(Headers.of(headers))
//                .addHeader("Authorization", auth)
//                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();
        log.info("\nkey: {}\nsecret: {}\nreq:\n\n{} {} \n{} \n{}\n",key,secret, request.method(), request.url(), header_str, content);
        Response response = client.newCall(request).execute();
        System.out.println("statusCode: "+response.code());
        return response.body().string();
    }

    private static OkHttpClient okHttpClient() {
        return new OkHttpClient().newBuilder()
                .readTimeout(Duration.ofSeconds(30))
                .connectTimeout(Duration.ofSeconds(30))
                .writeTimeout(Duration.ofSeconds(15))
                .build();
    }

}